#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  app.py
#       Author @  Huoty
#  Create date @  2015-12-09 10:49:15
#  Description @
# *************************************************************

from flask import Flask, render_template, request

# Script starts from here

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/login", methods=["POST", "GET"])
def login():
    error = "Unknown error"
    if request.method == "POST":
        if valid_login(request.form["username"], request.form["password"]):
            return log_the_user_in(request.form["username"])
        else:
            error = "Invalid username/password"

    return render_template('error.html', error=error)

def valid_login(username, password):
    userlist = { "admin":"123456", "huoty":"123456", "konghy":"123456" }
    if userlist.get(username) == password:
        return True
    else:
        return False

def log_the_user_in(username):
    return render_template("success.html", username=username)

if __name__ == "__main__":
    app.run(port=12508, debug=True)
